﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace TYBase
{
    public static class DataExtend
    {
        public static long NullAs(this long? dt, long defaultVal = 0)
        {
            if (dt == null)
            {
                return defaultVal;
            }
            return dt.Value;
        }
        static DateTime dt = new DateTime(1970, 1, 1);

        public static string NullAsTimeStr(this long? dt)
        {
            if (dt == null)
            {
                return string.Empty;
            }
            return dt.AsTime().AsString();
        }

        public static string IfAs(this int data, int IfValue, string res)
        {
            if (data == IfValue)
            {
                return res;
            }
            return data.ToString();
        }

        public static int IfNotAs(this int data, int minVal, int maxValue, int res)
        {
            if (data >= minVal && data <= maxValue)
            {
                return data;
            }
            return res;
        }

        public static int NullAs(this int? value, int defaultVal = 0)
        {
            if (value == null)
            { return defaultVal; }
            return value.Value;
        }
        public static string AsQueryData<T>(this T t)
        {
            string tStr = string.Empty;
            if (t == null)
            {
                return tStr;
            }
            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return tStr;
            }
            foreach (System.Reflection.PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(t, null);
                if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                {
                    tStr += $"{RemoveChar(name)}={RemoveChar($"{value}")}&";
                }


            }
            if (tStr.Length > 0)
            {
                tStr = tStr.Remove(tStr.Length - 1, 1);
            }
            return tStr;
        }
        private static string RemoveChar(string str)
        {
            return Uri.EscapeDataString(str);
            /// %?
            //=%2F%20%25%3F
            return str.Replace("/", "%2F")
                .Replace(" ", "%20")
                .Replace("%", "%25")
                .Replace("?", "%3F");
        }
        public static Dictionary<string, string> AsDictionary<T>(this T t)
        {
            Dictionary<string, string> dics = new Dictionary<string, string>();

            if (t == null)
            {
                return dics;
            }
            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return dics;
            }
            foreach (System.Reflection.PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(t, null);
                if (item.PropertyType.Name.StartsWith(value: "DateTime"))
                {
                    dics.Add(RemoveChar(name), RemoveChar($"{value}"));
                }
                else if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                {

                    dics.Add(RemoveChar(name), RemoveChar($"{value}"));

                }
            }
            return dics;
        }
    }
}
